<template>
  <div>
    <div class="login-header" />
    <div class="login-container">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">Rabbit测试平台</h3>
        </div>
        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            auto-complete="on"
          >
            <svg-icon slot="prefix" icon-class="user" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" />
            <svg-icon
              slot="suffix"
              style=" margin-right: 8px;"
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              @mousedown.native="showPwd"
              @mouseup.native="showPwd"
            />
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.checkbox">记住密码</el-checkbox>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:45px;margin-top:22px;"
          @click.native.prevent="handleLogin"
        >登陆</el-button>
      </el-form>
    </div>
    <div class="login-foot" />
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        checkbox: true
      },
      loginRules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      passwordKey: ')q4$%^)*&^%m',
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    const login = JSON.parse(localStorage.getItem('login'))
    if (login) {
      const _password = window.atob(login.p).replace(this.passwordKey + '', '')
      this.loginForm.username = login.u
      this.loginForm.password = _password
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      // this.$nextTick(() => {
      //   this.$refs.password.focus()
      // })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
            if (this.loginForm.checkbox) {
              const _password = window.btoa(this.loginForm.password + this.passwordKey)
              localStorage.setItem('login', JSON.stringify({ 'u': this.loginForm.username, 'p': _password }))
            } else {
              localStorage.removeItem('login', this.loginForm.checkbox)
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
$bg: #30aceb; // 登陆背景颜色
$dark_gray: #b3bcbe; // 图标颜色
$title_color: rgb(46, 45, 45); // title颜色
$input_color: rgb(24, 23, 23); // input颜色
$head_foot_bg: #ffffff;

.login-header {
  width: 100%;
  height: 72px;
  background: $head_foot_bg;
}
.login-foot {
  width: 100%;
  height: 72px;
  background: $head_foot_bg;
}
.login-container {
  // min-height: 100%;
  height: 600px;
  width: 100%;
  min-width: 420px;
  // background-color: $bg;
  overflow: hidden;
  background: linear-gradient(125deg, #8ed8fd, #0397fa) no-repeat;
  .login-form {
    // position: relative;
    width: 388px;
    max-width: 100%;
    padding: 30px 35px 55px 35px;
    float: right;
    // margin: 0 auto; // 居中显示
    margin-right: 11%;
    margin-top: 6%;
    background-color: rgb(255, 255, 255);
    overflow: hidden;
  }

  .el-input__icon {
    // width: 25px;
    color: $dark_gray;
  }
  .svg-icon {
    font-size: 15px;
    color: $dark_gray;
    margin-left: 8px;
  }

  // title样式
  .title-container {
    position: relative;

    .title {
      font-size: 20px;
      color: $title_color;
      margin: 10px auto 30px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>
